package com.myapplication.zhou.recycleviewscroll;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoushikun on 2016/7/5.
 */
public class recycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Object> list = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    private int TYPE_LIST = 0;
    private int TYPE_FOOT = 1;

    public recycleViewAdapter(Context context, List<Object> list) {
        this.list = list;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_LIST) {
            View view = inflater.inflate(R.layout.cardview, parent, false);
            ContentViewHolder contentViewHolder = new ContentViewHolder(view);
            return contentViewHolder;
        } else {
            View view = inflater.inflate(R.layout.footer, parent, false);
            Footer footer = new Footer(view);
            return footer;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentViewHolder) {
            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
            model model = (com.myapplication.zhou.recycleviewscroll.model) list.get(position);
            contentViewHolder.textView.setText(model.getText());
        } else if (holder instanceof Footer) {
            Footer footer = (Footer) holder;
            footer.textView.setText("jiazai zhong ....");
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (list != null) {
            if (list.get(position) instanceof model) {
                return TYPE_LIST;
            } else
                return TYPE_FOOT;
        }
        return -1;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ContentViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;

        public ContentViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text);
        }
    }

    public class Footer extends RecyclerView.ViewHolder {
        private TextView textView;

        public Footer(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textView);
        }
    }
}
